package com.example.demotencentbigdata.tchouse;

import com.example.demotencentbigdata.api.JDBCApi;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 【TChouse环境】
 * 中电金信实例
 * 1. 访问地址
 * http://114.132.63.187:8080
 * 2. 实例管理员
 * user：tbds_admin_zdjx
 * password：tbdsTBDSzdjx@%$
 *
 *
 * host:175.178.252.223/42.194.251.236
 * port:11427
 * dbname:postgres(默认库，需新建库操作)
 * user:tbase(超级管理员用户,谨慎操作,可新建用户给到业务)
 * password:TCh_Ouse_zdjx_@%$
 *
 */
@Component
public class TCHouse implements JDBCApi {
    @Override
    public List<HashMap<String, Object>> testQuery(Connection connection, String querySql) throws SQLException {
        List<HashMap<String,Object>> datas = new ArrayList<>();
        // Class.forName("");
        try(
            PreparedStatement preparedStatement = connection.prepareStatement(querySql);
            ResultSet resultSet = preparedStatement.executeQuery();) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            while (resultSet.next()) {
                HashMap<String,Object> data = new HashMap<>();
                for (int i = 0; i < columnCount; i++) {
                    data.put(metaData.getColumnName(i+1),resultSet.getObject(i+1));
                }
                datas.add(data);
            }
            return datas;
        }
    }

    @Override
    public Connection getConnect(String url, String userName, String passWd) throws SQLException {
        return DriverManager.getConnection(url, userName, passWd);
    }

    @Override
    public int update(Connection connection, String updateSql) throws SQLException {
        try(
                PreparedStatement preparedStatement = connection.prepareStatement(updateSql)) {
                return preparedStatement.executeUpdate();
        }
    }
}
